<@s.url var="actionUrl" action="tree" namespace="/widgets" />

<script>
	var tree;
	$(document).ready(function() {
		<#if parameters.targetId?exists>
			var obj = $("#${parameters.targetId}");
			tree = obj.ajaxTree({
				<#if parameters.datafile?exists>
					url:'${parameters.datafile}',
				<#else>
					url:'${actionUrl}',
					beforeLoad : function(e, treeNode, tree){
						tree.setLoadParas({
							id : treeNode.data.id
							<#if parameters.treeModelBean?exists>
								,treeModelBean:'${parameters.treeModelBean}'
							</#if>
							<#if parameters.rootId?exists>
								,rootId:'${parameters.rootId}'	//根节点ID
							</#if>
							<#if parameters.href?exists>
								,href:'${parameters.href}'  //URL链接
							</#if>
							//增加是否需要复选框参数
							<#if parameters.withCheckBox?exists>
								,withCheckBox:'${parameters.withCheckBox}'  //URL链接
							</#if>
						});
					}
				</#if>
			
				<#if parameters.hrefTarget?exists>
					,hrefTarget:'${parameters.hrefTarget}'  //目标链接
				</#if>
				,autoExpandRoot :${parameters.autoExpandRoot?default(true)?string}//是否展开根节点，默认为true	
			});
			tree.bind('nodeClick',function(e, treeNode){
				var nodeId = treeNode.data.id;
				obj.publish("tree-nodeSelect", {id: nodeId});
				obj.data("selected", treeNode.data);
			});
			
			//复选框选择事件（add by jiangw 2010.04.22）
			tree.bind('checkchange',function(e,treeNode){
			    //全选/反选
			    treeNode.expand();
				treeNode.eachChild(function(child){
				    var checked = treeNode.data.checked;
					child.checkChange(checked,true);
				});
				
			    var nodes = tree.getCheckedNode();
				var values = new Array();
				var i = 0;
				//父节点只做显示，需要保存的数据是最终子节点
				$.each(nodes, function() {         
                    if(this.data.leaf){//当前节点无子节点
				    values[i] = this.data.id;
				    i++;
				  }          
                 }); 
				//obj.publish("tree-nodeChecked", {checkedValues: values});
				obj.data("checkedValues", values);
			});
	
		</#if>
	}); // ready
</script>